# -*- coding: utf-8 -*-

import scrapy

import requests
from bs4 import BeautifulSoup
from datascrapy.items import DbSlowSqlItem
from datascrapy.settings import URL
from datascrapy.settings import COOKIE
from datascrapy.settings import HEADERS
from datascrapy.settings import DATAHEADERS
from datascrapy.settings import SYSNAME
from datascrapy.settings import ALLOWED_DOMAINS
from datascrapy.settings import START_URLS
from datascrapy.util import cookieutil


class SlowsqlSpider(scrapy.Spider):
    name = 'slowsql'
    allowed_domains = ALLOWED_DOMAINS
    start_urls = START_URLS

    def parse(self, response):
        response = requests.get(URL, headers=HEADERS)
        cookieutil.set_response_cookie(response)
        soup = BeautifulSoup(response.text, "html.parser")
        span = soup.find("span", text=SYSNAME)
        a_list = span.findParent().findParent().select('a[href^="/getfun/default"]')

        for a in a_list:
            href = a.attrs['href']
            href_str = str(href)
            href_str_id_end = href_str.split("?")[1]
            params = href_str_id_end.split("&")
            nei_count = 0
            id = ""
            host = ""
            name = ""
            for param in params:
                if nei_count == 0:
                    id = param.split("=", 1)[1]
                if nei_count == 2:
                    name = param.split("=", 1)[1]
                if nei_count == 3:
                    host = param.split("=", 1)[1]
                nei_count = nei_count + 1

            is_main_db_url = "http://dbms.itsm.cxxxxs.com/top-sql/single?" + "db=" + id + "&type=3" + "&host=" + host + "&name=" + name
            DATAHEADERS["Referer"] = is_main_db_url
            DATAHEADERS["Cookie"] = COOKIE + cookieutil.get_response_cookie()
            is_main_response = requests.post(is_main_db_url, data=get_payload_param(), headers=DATAHEADERS)
            cookieutil.set_response_cookie(is_main_response)
            is_main_response_data = is_main_response.json()
            for valueList in is_main_response_data["aaData"]:
                db_slow_sql = DbSlowSqlItem()
                db_slow_sql["sysName"] = SYSNAME
                db_slow_sql["ip"] = host
                db_slow_sql["sqlId"] = valueList[0]
                db_slow_sql["startTime"] = valueList[1]
                db_slow_sql["newestStartTime"] = valueList[2]
                db_slow_sql["exeNum"] = valueList[3]
                db_slow_sql["selSumTime"] = valueList[4]
                db_slow_sql["selTheShortest"] = valueList[5]
                db_slow_sql["selTheLongest"] = valueList[6]
                db_slow_sql["selMaxRow"] = valueList[7]
                db_slow_sql["sqlContext"] = valueList[len(valueList) - 1]
                yield db_slow_sql


def get_payload_param():
    json_str = '[{"name": "draw", "value": 1},{"name": "columns", "value": [{"data": 0, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 1, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 2, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 3, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 4, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 5, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 6, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 7, "name": "", "searchable": true, "orderable": true, "search": {"value": "", "search": false}},{"data": 8, "name": "", "searchable": true, "orderable": false, "search": {"value": "", "search": false}},{"data": 9, "name": "", "searchable": true, "orderable": false, "search": {"value": "", "search": false}}]},{"name": "order", "value": [{"column": 6, "dir": "desc"}]},{"name": "start", "value": 0},{"name": "length", "value": 20},{"name": "search", "value": {"value": "", "search": false}},{"fliter": {"classify": "common", "runtime": "7", "outtime": "", "keytext": ""}}]'
    return json_str
